package com.example.Gui;

import com.example.AdminComponent.Course;
import com.example.AdminComponent.PassJFrame;
import com.example.AdminComponent.Student;
import com.example.AdminComponent.Teacher;
import com.example.TeacherComponent.TeacherComponentCourse;
import com.example.TeacherComponent.TeacherComponentStudent;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TeacherUi {
    //1.给学生打分
    //2.看到自己的上课时间
    //3.请求调课
    JFrame jFrame=new JFrame("欢迎进入教师服务界面");
    private String user;
    private String passWd;


    void init(String user,String passWd) {
        this.user = user;
        this.passWd = passWd;
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
        jFrame.setSize(900, 500);
        jFrame.setLocationRelativeTo(null);
        jFrame.setResizable(false);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //设置菜单栏
        JMenuBar jmb = new JMenuBar();  //菜单条
        JMenu jMenu = new JMenu("设置");
        JMenuItem m1 = new JMenuItem("切换账号");
        JMenuItem m2 = new JMenuItem("退出程序");
        JMenuItem m3 = new JMenuItem("修改密码");
        //点击切换账号
        m1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new LoginUI().init();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                jFrame.dispose();
            }
        });

        //点击退出程序
        m2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        //点击修改密码
        m3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new PassJFrame(user, passWd);
            }
        });
        jMenu.add(m1);
        jMenu.add(m2);
        jMenu.add(m3);
        jmb.add(jMenu);
        jFrame.setJMenuBar(jmb);

        //设置分割面板
        JSplitPane jSplitPane = new JSplitPane();
        //支持连续布局
        jSplitPane.setContinuousLayout(true);
        jSplitPane.setDividerLocation(150);
        jSplitPane.setDividerSize(5);
        //设置左侧内容
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("教师服务");
        DefaultMutableTreeNode student = new DefaultMutableTreeNode("学生管理");
        DefaultMutableTreeNode course = new DefaultMutableTreeNode("课程管理");

        root.add(student);
        root.add(course);
        JTree tree = new JTree(root);
        jSplitPane.setLeftComponent(tree);
        //监听student
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                Object lastPathComponent = e.getNewLeadSelectionPath().getLastPathComponent();
                if (lastPathComponent.equals(student)) {
                    //默认左右宽度
                    jSplitPane.setDividerLocation(150);
                    jSplitPane.setRightComponent(new TeacherComponentStudent(user));

                } else if (lastPathComponent.equals(course)) {
                    jSplitPane.setDividerLocation(150);
                    jSplitPane.setRightComponent(new TeacherComponentCourse(user));

                }
            }
        });

        //设置tree默认选中学生管理
        tree.setSelectionRow(1);
        jFrame.add(jSplitPane);
    }
}
